<?php
class baseController extends controller{
	protected $appConfig = array();
	
	public function __construct(){
		$this->appConfig = config('APP');
		if( $this->_readHtmlCache() ){
			$this->appConfig['HTML_CACHE_ON'] = false;
			exit;
		}
		parent::__construct();
		@session_start();
	}
	
	public function __destruct(){
		$this->_writeHtmlCache();
	}
	
	//读取静态缓存
	private function _readHtmlCache() {	
		if ( ($this->appConfig['HTML_CACHE_ON'] == false) || empty($this->appConfig['HTML_CACHE_RULE']) ) {
			$this->appConfig['HTML_CACHE_ON'] = false;
			return false;
		}
		if( isset($this->appConfig['HTML_CACHE_RULE'][APP_NAME][CONTROLLER_NAME][ACTION_NAME]) ){
			$expire = $this->appConfig['HTML_CACHE_RULE'][APP_NAME][CONTROLLER_NAME][ACTION_NAME];
		}else if(isset($this->appConfig['HTML_CACHE_RULE'][APP_NAME][CONTROLLER_NAME]['*'])){
			$expire = $this->appConfig['HTML_CACHE_RULE'][APP_NAME][CONTROLLER_NAME]['*'];
		}else{
			$this->appConfig['HTML_CACHE_ON'] = false;
			return false;
		}
		return cpHtmlCache::read($this->appConfig['HTML_CACHE_PATH'], $expire);
	}
	
	//写入静态页面缓存
	private function _writeHtmlCache() {	
		if ( $this->appConfig['HTML_CACHE_ON'] ) {
			cpHtmlCache::write();
		}	
	}

	//提示
    public function msg($message,$status=1,$ajax=false) {
    	if($status){
    		$status='y';
    	}else{
    		$status='n';
    	}
        if (is_ajax()||$ajax){
            @header("Content-type:text/html");
            echo json_encode(array('status' => $status, 'info' => $message));
            exit;
        }else{
            $this->error($message,$status);
        } 
    }

    //错误页面
    public function error($message,$status='n') {
		$html='
		<!DOCTYPE html>
		<html>
		<head>
		<meta charset="utf-8"/>
		';
		if($status=='y'){
			$html.='<title>处理成功！</title>';
		}else{
			$html.='<title>处理失败，发生错误！</title>';
		}
		$html.='<meta name="keywords" content=""/>
		<meta name="description" content=""/>
		<meta name="viewport" content="width=device-width"/>
		<link href="'.__PUBLIC__.'/ui/artdialog/skins/default.css" rel="stylesheet" type="text/css">
		<script src="'.__PUBLIC__.'/ui/jquery.js"></script>
		<script src="'.__PUBLIC__.'/ui/artdialog/artDialog.js"></script>
		</head>
		<body>

		<script>
		art.dialog({
			content:"'.$message.'",
			';
			if($status=='y'){
				$html.='icon: "succeed",';
			}else{
				$html.='icon: "error",';
			}
		$html.='cancelVal: \'确认\',
			cancel:function () {
	    	history.back();
	        return false;
	    	}
		});
		</script>
		</body>
		</html>
    	';
    	echo $html;
    	exit;
    }

}